/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bombercloud.client;

import bombercloud.commons.BlocoQuebra;
import bombercloud.commons.Boneco;
import bombercloud.commons.Request;
import bombercloud.commons.Response;
import java.awt.Color;
import java.awt.Font;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Delano
 */
public class InterfaceGanhador extends JFrame {

    JPanel mainPanel;
    JButton botaoFinalizar, botaoBonecoBranco, botaoBonecoPreto, botaoBonecoAzul, botaoBonecoVermelho, botaoBonecoVerde;
    JLabel labelIniciar, jLabelGanhador, jLabelTexto;
    Boneco boneco = new Boneco();
    private ArrayList<Boneco> listBonecos = new ArrayList<Boneco>();
    private Socket socket;
    private ObjectInputStream inputStream;
    private ObjectOutputStream outputStream;
    String usuarioPC;

    public InterfaceGanhador(Socket socket1, ObjectOutputStream objectOutputStream, ObjectInputStream inputStream1, Boneco bonecoGanhador, String usuario) throws HeadlessException {
        setSize(410, 315);
        setLocationRelativeTo(null);
        setLayout(null);
        setResizable(false);
        this.setTitle("Vencedor");

        mainPanel = (JPanel) this.getContentPane();
//        mainPanel.setBackground(Color.decode("#004000"));

        usuarioPC = usuario;
        socket = socket1;
        outputStream = objectOutputStream;
        inputStream = inputStream1;
        boneco = bonecoGanhador;

        if (boneco != null) {
            criacaoDaTelaComGanhador(mainPanel);
            System.out.println("com ganhador");
        } else {
            criacaoDaTelaEmpate(mainPanel);
        }
        declaraEventos(this);
//        this.addKeyListener(this);
//        eventos();
    }

    private void criacaoDaTelaComGanhador(JPanel mainPanel) {

        Font fonte = new Font("Tahoma", Font.BOLD, 24);
        Font fonte2 = new Font("Tahoma", Font.BOLD, 12);


        labelIniciar = new JLabel("Bomber Cloud");
        labelIniciar.setSize(168, 29);
        labelIniciar.setLocation(120, 20);
        labelIniciar.setFont(fonte);
        mainPanel.add(labelIniciar);


        jLabelGanhador = new JLabel(new ImageIcon(boneco.getImagemPLACAR().replaceAll("server", "client").replaceAll("Delano", usuarioPC)));
//        jLabelGanhador.setFont(fonte2);
        jLabelGanhador.setSize(48, 40);
        jLabelGanhador.setLocation(140, 100);
        mainPanel.add(jLabelGanhador);

        jLabelTexto = new JLabel("Venceu!");
        jLabelTexto.setFont(fonte);
        jLabelTexto.setForeground(Color.GREEN);
        jLabelTexto.setSize(100, 30);
        jLabelTexto.setLocation(200, 100);
        jLabelTexto.setEnabled(false);
        mainPanel.add(jLabelTexto);

        botaoFinalizar = new JButton("Finalizar");
        botaoFinalizar.setFont(fonte2);
        botaoFinalizar.setSize(120, 25);
        botaoFinalizar.setLocation(140, 160);
        mainPanel.add(botaoFinalizar);



        botaoBonecoBranco = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "BRANCO_FOTO.png"));
        botaoBonecoBranco.setSize(48, 40);
        botaoBonecoBranco.setLocation(30, 200);
        botaoBonecoBranco.setEnabled(false);
//        botaoBonecoBranco.setBackground(Color.decode("#0066FF"));
        mainPanel.add(botaoBonecoBranco);

        botaoBonecoPreto = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "PRETO_FOTO.png"));
        botaoBonecoPreto.setSize(48, 40);
        botaoBonecoPreto.setLocation(100, 200);
        botaoBonecoPreto.setEnabled(false);
        mainPanel.add(botaoBonecoPreto);

        botaoBonecoAzul = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "AZUL_FOTO.png"));
        botaoBonecoAzul.setSize(48, 40);
        botaoBonecoAzul.setLocation(170, 200);
        botaoBonecoAzul.setEnabled(false);
        mainPanel.add(botaoBonecoAzul);

        botaoBonecoVermelho = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "VERMELHO_FOTO.png"));
        botaoBonecoVermelho.setSize(48, 40);
        botaoBonecoVermelho.setLocation(240, 200);
        botaoBonecoVermelho.setEnabled(false);
        mainPanel.add(botaoBonecoVermelho);

        botaoBonecoVerde = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "VERDE_FOTO.png"));
        botaoBonecoVerde.setSize(48, 40);
        botaoBonecoVerde.setLocation(310, 200);
        botaoBonecoVerde.setEnabled(false);
        mainPanel.add(botaoBonecoVerde);


    }

    private void criacaoDaTelaEmpate(JPanel mainPanel) {

        Font fonte = new Font("Tahoma", Font.BOLD, 24);
        Font fonte2 = new Font("Tahoma", Font.BOLD, 12);

        labelIniciar = new JLabel("Bomber Cloud");
        labelIniciar.setSize(168, 29);
        labelIniciar.setLocation(120, 20);
        labelIniciar.setFont(fonte);
        mainPanel.add(labelIniciar);

        jLabelGanhador = new JLabel("Empate!");
        jLabelGanhador.setFont(fonte2);
        jLabelGanhador.setSize(180, 30);
        jLabelGanhador.setLocation(180, 100);
        mainPanel.add(jLabelGanhador);

        botaoFinalizar = new JButton("Finalizar");
        botaoFinalizar.setFont(fonte2);
        botaoFinalizar.setSize(120, 25);
        botaoFinalizar.setLocation(140, 160);
        mainPanel.add(botaoFinalizar);

        botaoBonecoBranco = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "BRANCO_FOTO.png"));
        botaoBonecoBranco.setSize(48, 40);
        botaoBonecoBranco.setLocation(30, 200);
        botaoBonecoBranco.setEnabled(false);
        mainPanel.add(botaoBonecoBranco);

        botaoBonecoPreto = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "PRETO_FOTO.png"));
        botaoBonecoPreto.setSize(48, 40);
        botaoBonecoPreto.setLocation(100, 200);
        botaoBonecoPreto.setEnabled(false);
        mainPanel.add(botaoBonecoPreto);

        botaoBonecoAzul = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "AZUL_FOTO.png"));
        botaoBonecoAzul.setSize(48, 40);
        botaoBonecoAzul.setLocation(170, 200);
        botaoBonecoAzul.setEnabled(false);
        mainPanel.add(botaoBonecoAzul);

        botaoBonecoVermelho = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "VERMELHO_FOTO.png"));
        botaoBonecoVermelho.setSize(48, 40);
        botaoBonecoVermelho.setLocation(240, 200);
        botaoBonecoVermelho.setEnabled(false);
        mainPanel.add(botaoBonecoVermelho);

        botaoBonecoVerde = new JButton(new ImageIcon(System.getProperty("user.dir") + System.getProperty("file.separator") + "src" + System.getProperty("file.separator") + "images" + System.getProperty("file.separator") + "VERDE_FOTO.png"));
        botaoBonecoVerde.setSize(48, 40);
        botaoBonecoVerde.setLocation(310, 200);
        botaoBonecoVerde.setEnabled(false);
        mainPanel.add(botaoBonecoVerde);
    }

    private void declaraEventos(final JFrame b) {
        // Eventos de Janela JFrame

        this.addWindowListener(
                new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }
                });

        //ao clicar nesse botao, pega o socket, o output e o input criado, assim como o número do boneco escolhido
        //e passa como parametro para a a UI que criará o tabuleiro com a informação de qual boneco foi escolhido.
        botaoFinalizar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Request request = new Request();
                request.setType(Request.REQUEST_TYPE_FINALIZA);
                executeRequest(request);
            }
        });

    }

    public void finalizaPartida(InterfaceGanhador t, ThreadClient tr) {
        t.setVisible(false);
        BomberSegundo b = new BomberSegundo(tr.getSocket(), tr.getOutputStream(), tr.getInputStream(), usuarioPC);
        b.setVisible(true);
        tr.setSocket(b.getSocket());
        tr.setInputStream(b.getInputStream());
        tr.setOutputStream(b.getOutputStream());
        tr.setRefBomberSegundo(b);
        tr.setRefBomber(null);      

//        bc.montarTabuleiro();

    }

    public synchronized Response executeRequest(Request request) {
        Response response = null;
        try {
            outputStream.writeObject(request);
            outputStream.flush();
//            response = (Response) inputStream.readObject();
        } //        catch (ClassNotFoundException ex) {
        //            System.out.println(ex.getMessage());
        //        }
        catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
        return response;
    }

    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    public ObjectInputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(ObjectInputStream inputStream) {
        this.inputStream = inputStream;
    }

    public ObjectOutputStream getOutputStream() {
        return outputStream;
    }

    public void setOutputStream(ObjectOutputStream outputStream) {
        this.outputStream = outputStream;
    }
}
